package com.king.service.impl;

import com.king.dao.UserRepository;
import com.king.pojo.entity.SysUser;
import com.king.pojo.query.UserQuery;
import com.king.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liukai
 */
@Service
public class UserServiceImpl implements UserService {

    private final String DEFAULT_PASSWORD = "123456";

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public SysUser insert(SysUser user) {
        user.setPassword(passwordEncoder.encode(DEFAULT_PASSWORD));
        return userRepository.save(user);
    }

    @Override
    public Page<SysUser> listUser(UserQuery userQuery, Pageable pageable) {
        return userRepository.findAll(pageable);
    }

    @Override
    public List<SysUser> listUser(SysUser user) {
        return userRepository.findAll();
    }

    @Override
    public void savePassword(String userId) {
        SysUser user = userRepository.getOne(userId);
        user.setPassword(passwordEncoder.encode(DEFAULT_PASSWORD));
        userRepository.save(user);
    }

}
